package com.xiaoqi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xiaoqi.constant.MessageConstant;
import com.xiaoqi.constant.RedisMessageConstant;
import com.xiaoqi.pojo.Result;
import com.xiaoqi.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @author PC
 * creationTime: 2022/11/21 11:34
 */
@RequestMapping("/order")
@RestController
public class OrderMobileController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;


    /**
     * 提交预约
     * @param datemap
     * @return
     */
    @PostMapping("/submit")
    private Result submit(@RequestBody Map<String,String> datemap){
        //获取jedis对象
        Jedis jedis = jedisPool.getResource();
        //获取手机号拼接成key
        String telephone = RedisMessageConstant.SENDTYPE_ORDER+"_"+datemap.get("telephone");
        //用户填的验证码
        String validateCode = datemap.get("validateCode");
        //从redis中查询此key
        String CodeInRedis = jedis.get(telephone);
        //判断key是否为空
        if (StringUtils.isEmpty(CodeInRedis)){
            return new Result(false, MessageConstant.RESENT_VALIDATECODE);
        }
        //验证码不为空则判断验证码是否正确
        if (!CodeInRedis.equals("validateCode")){
            //不匹配返回message
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        return null;
    }

    /**
     * 根据订单id查询预约信息
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam(value = "id") Integer id){
        Map map = orderService.findById4Detail(id);
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
    }
}
